package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.GPSUtil;
import com.kmproject.haifu.di.component.DaggerMapOneShowComponent;
import com.kmproject.haifu.mvp.contract.MapOneShowContract;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.presenter.MapOneShowPresenter;
import com.kmproject.haifu.mvp.ui.adapter.CustomInfoMapWindowAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/16/2019 09:28
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MapOneShowActivity extends BaseActivity<MapOneShowPresenter> implements MapOneShowContract.View, OnMapReadyCallback {

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.mapview)
    MapView mapview;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMapOneShowComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_map_one_show; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        try {
            MapsInitializer.initialize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (ConnectionResult.SUCCESS != errorCode) {
            GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
        } else {
            mapview.getMapAsync(this);

        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mapview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapview!=null)
        {
            mapview.onDestroy();
        }

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        mapview.onCreate(savedInstanceState);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
    googleMap.setMyLocationEnabled(true);
    String lat=getIntent().getStringExtra("lat");
    String lgt=getIntent().getStringExtra("lgt");
   if (lat==null||lgt==null||TextUtils.isEmpty(lgt)||TextUtils.isEmpty(lat))
   {
       showMessage("经纬度异常");
       killMyself();
       return;
   }
     double[] temLatLgt=GPSUtil.gps84_To_Gcj02(Double.parseDouble(lat),Double.parseDouble(lgt));
        LatLng appointLoc = new LatLng(temLatLgt[0], temLatLgt[1]);
        // 移动地图到指定经度的位置
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(appointLoc,12));
//        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(appointLoc, 12));
        googleMap.setInfoWindowAdapter(new CustomInfoMapWindowAdapter(MapOneShowActivity.this));
        Marker smarker=    googleMap.addMarker(new MarkerOptions().position(appointLoc).title("标题").snippet("描述").visible(true)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_maks_icon)));

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                smarker.showInfoWindow();
            }
        });

    }
}
